package com.chenfwind.bookclub.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chenfwind.bookclub.R;

/**
 * Created by mufeng on 2016/2/15.
 */
public class login extends ActionBarActivity {

    private Button loginButton;
    EditText bcUsername;
    EditText bcPassword;
    TextView toRegister;

    private String username = "";
    private String password = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        toRegister = ( TextView ) findViewById(R.id.id_tv_register);
        toRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(login.this , MainActivity.class);
                startActivity(intent);
            }
        });
        /**
         * 1.初始化当前所需控件，如何出事话一个控件？
         *   findViewById---返回的时一个view对象
         *   findViewById是如何查找对应VIew的id
         *  2.设置button的监听器，通过监听器实现我们点击button要操作的事情
         * **/
        loginButton = (Button) findViewById(R.id.login_subBtn);
        /**
        * 1.监听事件通过第一种实现实现（匿名内部类）
        * */
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在当前onClick方法中监听点击Button的动作
//                if ( prepareForLogin()){
//                    return;
//                }
               Log.e("HHH","GGGG");
//                System.out.print(bcUsername.length());
                bcUsername = (EditText) findViewById(R.id.login_accountEt);
                bcPassword = (EditText) findViewById(R.id.login_pwdEt);

                username = bcUsername.getText().toString();
                password = bcPassword.getText().toString();

                Toast.makeText(login.this, username, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public boolean prepareForLogin(){
//        if (bcUsername.length() == 0) {
//            bcUsername.setError("请输入邮箱/用户名");
//            bcUsername.requestFocus();
//            return true;
//        }
//        if (bcPassword.length() == 0) {
//            bcPassword.setError("请输入密码");
//            bcPassword.requestFocus();
//            return true;
//        }

        return false;
    }
}